const router = require('koa-router')()
const mongoose = require('mongoose');
const user = require('./../model/user');
// const good = require('./../model/goods');
// const orderList = require('./../model/orderList');
mongoose.connect('mongodb://127.0.0.1:27017/shenghuo',{ useNewUrlParser: true });
mongoose.connection.once('open',()=>{
    console.log('数据库连接成功')
});
mongoose.connection.on('error',()=>{
    console.log('数据库连接失败')
})
//用户注册接口
router.post('/user',async (ctx,next)=>{
    let name=ctx.request.body.name;
    console.log(name)
    let userName=ctx.request.body.username;
    let password=ctx.request.body.password;
    let doc;
    let userId=0;
    doc=await user.findOne({userName});
    if(doc){
        ctx.body={
            status:2,
            msg:'',
            result:'账号已经存在'
        }
    }else{
        const users=new user({
            userId:userId++,
            name,
            userName,
            password
        });
        doc=await users.save();
        if(doc){
            ctx.body={
                status:1,
                msg:'',
                result:'注册成功'
            }
        }else{
            ctx.body={
                status:0,
                msg:'',
                result:'服务器出错'
            }
        }
    }
   
})
//用户登录接口
router.post('/login',async (ctx,next)=>{
    let userName=ctx.request.body.userName;
    let password=ctx.request.body.password;
    let doc=await user.findOne({userName});
    if(doc){
        if(doc.password==password){
            ctx.body={
                status:1,
                msg:'登陆成功',
                result:doc
            }
        }else{
            ctx.body={
                status:2,
                msg:'',
                result:"密码错误"
            }
        }
    }else{
        ctx.body={
            status:0,
            msg:'账号不存在',
            result:""
        }
    }
    
})
//查找用户资料
router.post('/userInfo',async (ctx,next)=>{
    let userName=ctx.request.body.userName;
    let doc=await user.findOne({userName});
    if(doc){
        ctx.body={
            status:200,
            msg:'OK',
            result:doc
            
        }
    }else{
        ctx.body={
            status:0,
            msg:'',
            result:'请先登录'
        }
    }
})

//更新用户资料
router.post('/updateInfo',async (ctx,next)=>{
    let userName=ctx.request.body.userName//用户账户
    let name=ctx.request.body.name;
    let heade_img=ctx.request.body.images;
    let sex=ctx.request.body.sex;
    let adress=ctx.request.body.adress;
    let personal_profile=ctx.request.body.personal_profile
    console.log(heade_img)
    let doc=await user.update({userName},{$set:{name,personal_profile,adress,sex,heade_img}});
    console.log(doc)
    
})
//兑换接口
router.post('/buy',async (ctx,next)=>{
    let id=ctx.request.body.id;
    let userName=ctx.request.body.userName;//账号
    let name=ctx.request.body.name;//姓名
    let phone=ctx.request.body.phone;//手机号
    let priec=ctx.request.body.priec;//兑换价格
    let doc=await user.findOne({userName});
    if(doc){
        let changpriec=doc.userInfo.integral-priec;
        doc.userInfo.integral=changpriec.toString();
        let godddoc=await good.findOne({_id:id})
        if(changpriec<=0){
            ctx.body={
                    status:3,
                    msg:'',
                    result:'sorry积分不够'
            }
            return
        }
        //积分足够新加订单
        let order=new orderList({
            id,
            userName,
            name,
            phone,
            priec,
            good:godddoc
        })
        let orderdoc=order.save()
        if(orderdoc){
             //兑换成功保存
            let sav=doc.save()
            if(sav){
                ctx.body={
                    status:1,
                    msg:'',
                    result:'兑换成功'
                }
            }
        }else{
            ctx.body={
                status:0,
                msg:'',
                result:'网络错误'
            }
        }
    }else{
        ctx.body={
            status:2,
            msg:'',
            result:'没有找到该用户'
        }
    }
})

//查找所有用户列表
router.get('/userAll',async (ctx,next)=>{
    let page=ctx.request.query.page
    let doc=await user.find().limit(10).skip(page*10);
    if(doc){
      ctx.body={
        status:1,
        msg:'',
        result:doc
      }
    }else{
      ctx.body={
        status:1,
        msg:'',
        result:'没有用户'
      }
    }
  })

  //修改用户积分
  router.post('/changeIntegral',async (ctx,next)=>{
        let id=ctx.request.body.id;
        let integral=ctx.request.body.integral;
        let doc=await user.findOne({_id:id});
        if(doc){
            doc.userInfo.integral=integral;
            let sav=doc.save()
            if(sav){
                ctx.body={
                    status:1,
                    msg:'',
                    result:"修改成功"
                }
            }
        }
  })


  //删除用户信息
  router.get('/deletUser',async (ctx,next)=>{
      let id=ctx.request.query.id;
      let doc=await user.remove({_id:id});
      console.log(doc)
      if(doc.n==1){
        ctx.body={
            status:1,
            msg:'',
            result:'删除成功'
        }
      }else{
        ctx.body={
            status:0,
            msg:'',
            result:'删除失败'
        }
      }
  })
module.exports = router;
